package com.news.yp.baselibrary.base;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * P 层需要的泛型是V
 *
 * @param <T>
 */
public class BasePresenter<T extends BaseView> {

    private T mProxyView;

    //这边需要一个View参数，不知道谁哪个View,使用泛型
    public void attach(final T mView) {
        //得到弱引用
        mProxyView = (T) Proxy.newProxyInstance(mView.getClass().getClassLoader(), mView.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                if (mView == null) {
                    return null;
                }
                return method.invoke(mView, objects);
            }
        });
    }

    public void detach() {
        mProxyView = null;
    }

    public T getView() {
        return mProxyView;
    }
}
